<template>
  <div class="pr w h container">
    <div class="login-box">
      <div class="glass"></div>
      <div class="main pr">
        <div class="title">Revol's Manage</div>
        <el-form ref="ruleFormRef" label-width="80px" :model="form" :rules="rules" class="form-box">
          <el-form-item label="手机号" prop="phone_num">
            <el-input v-model="form.phone_num" />
          </el-form-item>
          <el-form-item label="密码" prop="password">
            <el-input @keyup.enter="login" show-password v-model="form.password" />
          </el-form-item>
        </el-form>
        <el-row class="footer" justify="space-evenly">
          <el-col :span="10">
            <el-button class="w btn-submit" size="large" type="primary" @click="login" :loading="loading">登录</el-button>
          </el-col>
        </el-row>
      </div>
    </div>
  </div>
</template>

<script setup>
import to from 'await-to-js'
import Api from '@/api'
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'

import { useUserStore } from '@/store/modules/user'

const router = useRouter()
const userStore = useUserStore()

const ruleFormRef = ref()

const loading = ref(false)

const form = reactive({
  phone_num: '',
  password: ''
})

// 自定义手机号校验
const checkPhone = (rule, value, callback) => {
  if (!value) {
    return callback(new Error('请输入手机号'))
  }
  const reg = /^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]{8}$/
  if (!reg.test(value)) {
    return callback(new Error('请输入正确的手机号'))
  }
  callback()
}

// 自定义密码校验
const checkPass = (rule, value, callback) => {
  if (value === '') {
    callback(new Error('请输入密码'))
  } else {
    callback()
  }
}

// 表单校验规则集合
const rules = reactive({
  phone_num: [{ validator: checkPhone, trigger: 'blur' }],
  password: [{ validator: checkPass, trigger: 'blur' }]
})

/**表单验证
 * @param {*} formEl 表单组件ref
 * @return {*}
 */
const validateForm = formEl => {
  return new Promise(resolve => {
    if (!formEl) {
      resolve(false)
    }
    formEl.validate(valid => {
      if (valid) {
        resolve(true)
      } else {
        resolve(false)
      }
    })
  })
}

// 登录
const login = async () => {
  // 先校验
  const valid = await validateForm(ruleFormRef.value)
  if (!valid) return

  loading.value = true

  // 通过校验再调登录接口
  const [err, res] = await to(Api.auth.login(form))
  if (!err) {
    const { data } = res
    // 登录成功后，设置全局token和用户信息
    userStore.setToken(data.token)
    userStore.setUserInfo(data)
    router.push({
      path: '/'
    })
  }
  loading.value = false
}
</script>

<style lang="scss" scoped>
@import '@/styles/variables.scss';
$footHeight: 80px;
.container {
  display: flex;
  justify-content: flex-end;
  align-items: center;
  background: url('/assets/images/bg_login.jpg') center center no-repeat;
  background-size: cover;
  overflow: auto;
}

.login-box {
  position: relative;
  margin: 0 100px 0 0;
  width: 500px;
  min-width: 500px;
  height: 360px;
  border-radius: 20px;
  box-shadow: 0px 2px 15px rgba(0, 0, 0, 0.3);
  .glass {
    position: absolute;
    left: 0;
    top: 0;
    width: 100%;
    height: 100%;
    border-radius: 10px;
    background-color: rgba(0, 0, 0, 0.4);
  }
  .main {
    height: 100%;
    display: flex;
    flex-direction: column;
    padding: 10px 10px $footHeight;
    color: #ddd;
  }
  .title {
    font-size: 36px;
    text-align: center;
  }
  .form-box {
    flex: 1;
    padding: 30px 100px 30px 30px;
    display: flex;
    flex-direction: column;
    justify-content: center;
    --el-form-label-font-size: 18px;
    --el-text-color-regular: #ddd;
    :deep(.el-input__wrapper) {
      font-size: var(--el-form-label-font-size);
      background-color: transparent;
    }
  }
  .footer {
    position: absolute;
    left: 0;
    right: 0;
    bottom: 0;
    height: $footHeight;
  }
}
</style>
